const Article = require("../model/article");
const formidable = require('formidable'); //处理含有文件上传的表单
const path = require('path');
const uuid = require('uuid'); //产生唯一字符串
const fs = require('fs');
const { getNowFormatTime } = require('../util.js')
/**
 * 文章中间件
 */
module.exports = {
  // 获取全部文章
  getHot: (req, res, next) => {
    Article.getHot()
      .then((res) => {
        res.map(item => {
          item.time = item.time.toString();
        })
        req.hots = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  /**
   * 获取最新文章
   */
  getList: (req, res, next) => {
    Article.getList()
      .then((res) => {
        res.map(item => {
          item.time = item.time.toString();
        })
        req.article = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  getArticleList: (req, res, next) => {
    let { id } = req.query;
    Article.getArticleList(id).then((res) => {
      res.map(item => {
        item.time = item.time.toString();
      })
      req.articleList = res;
      next();
    }).catch(err => {
      next(err)
    });
  },
  // 获取分类目录下文章
  getListByCategoryId: (req, res, next) => {
    // 获取地址栏传递的id值
    let { category_id, hots, webPageId, keyword, size } = req.query;
    Article.getListByCategoryId(category_id, hots, webPageId, keyword)
      .then((res) => {
        let current = Math.ceil(res.length / size);
        res.map(item => {
          item.time = item.time.toString();
        })
        req.ListArticle = res;
        req.page = {
          total: res.length,
          current,
        };
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取指定关键字的文章列表
  getListByKeyword: (req, res, next) => {
    // 获取地址栏传递的id值
    let { keyword, current, size } = req.query;
    current = Number(current);
    size = Number(size)
    Article.getListByKeyword(keyword, current, size)
      .then((res) => {
        res.map(item => {
          item.time = item.time.toString();
        })
        req.keyword = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取上一篇文章
  getPrev: (req, res, next) => {
    // 获取地址栏传递的id值
    let { id } = req.query;
    Article.getPrevArticle(id)
      .then((res) => {
        res.map(item => {
          item.time = item.time.toString();
        })
        req.prevArticle = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取下一篇文章
  getNext: (req, res, next) => {
    // 获取地址栏传递的id值
    let { id } = req.query;
    Article.getNextArticle(id)
      .then((res) => {
        res.map(item => {
          item.time = item.time.toString();
        })
        req.nextArticle = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 获取指定文章列表，实现分页功能
  getPage: (req, res, next) => {
    let { start, size } = res;
    let { category_id, hots, webPageId, keyword } = req.query;
    Article.getPage(start, size, category_id, hots, webPageId, keyword)
      .then((res) => {
        res.map(item => {
          item.time = item.time.toString();
        })
        req.pageList = res;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 设置热门推荐
  setHot: (req, res, next) => {
    let { id, hot } = req.query;
    Article.setHot(id, hot)
      .then((resolve) => {
        req.affectedRows = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 文章添加
  articleAdd: (req, res, next) => {
    let article = req.query;
    article.time = getNowFormatTime();
    Article.articleAdd(article)
      .then((resolve) => {
        req.insertId = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
    // //处理含有文件上传的表单
    // var form = new formidable.IncomingForm();
    // form.uploadDir='static' 
    // //更改上传成功的临时文件的存放位置
    // let fullpath = path.resolve(__dirname, '../static/upload');
    // form.parse(req, function (err, fields, files) {
    //   if (!err) {
    //     // 判断是否有传递过来图片,有则对图片进行重命名
    //     if (files.description_image_path) {
    //       //获取文件扩展名
    //       let extname = path.extname(files.description_image_path.name);
    //       //完整文件名
    //       let fullname = uuid.v1() + extname;
    //       //将上传成功后的临时文件名改成正式文件名
    //       fs.renameSync(files.description_image_path.path, fullpath + '/' + fullname);
    //       //向fields对象中添加属性img
    //       fields.description_image_path = "/upload/" + fullname;
    //     }
    //   }
    // })
  },
  // 文章删除
  delArticle: (req, res, next) => {
    let { id } = req.body;
    Article.delArticle(id)
      .then((resolve) => {
        // resolve.map(item => {
        //   item.time = item.time.toString();
        // })
        req.affectedRows = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
  // 编辑文章
  editArticle: (req, res, next) => {
    let { title, content, hot, category_id, id, category_name, description_image_path } = req.query;
    time = getNowFormatTime();
    let article = {
      title,
      content,
      hot: hot ? 1 : 0,
      category_id,
      id,
      category_name,
      description_image_path,
      time
    };
    Article.editArticle(article)
      .then((resolve) => {
        // resolve.map(item => {
        //   item.time = item.time.toString();
        // })
        req.affectedRows = resolve;
        next();
      })
      .catch((err) => {
        next(err);
      });
  },
};
